<?php
/**
 * InPatientTicket.
 * @package core.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class InPatientTicket extends ORDataObject {
/**#@+
     * Fields of table: ticket mapped to class members
     */
    var $ticket_id      = '';
    var $department_id  = '';
    var $patient_id     = '';
    var $name           = '';   
    var $insurance      = '';
    var $examination_type_id = '';
    var $created        = '';
    var $started        = '';
    var $closed         = '';
    var $sale_program_store_id      = '';
    
    var $total_payment      = '';
    var $patient_payment        = '';
    var $patient_payment_without_discount = '';
    var $insurance_payment      = '';
    var $discount_payment       = '';
    var $prescription_payment       = '';
    
    var $real_payment       = '';
    var $status     = '';
    var $exam_cause = '';
    var $notes      = '';
    
    var $modifier_id = '';
    var $modified_date = '';
    var $enum_patient_type = '';
    /**#@-*/

    const   NEAR_EXAM = 17; 
    const   OLDER = 60;

    var $_ticket_prefix = "";
    /**
     * DB Table
     */
    var $_table = 'ticket';

    /**
     * Primary Key
     */
    var $_key = 'ticket_id';
    
    /**
     * Internal Name
     */
    var $_internalName = 'Ticket';

    const OPEN_STATUS = "open";
    const CLOSED_STATUS = "closed";
    
    /**
     * Handle instantiation
     */
    function Ticket() {
        parent::ORDataObject();
        $conf = Celini::configInstance();
        
        $this->_ticket_prefix = $conf->get('ticket_prefix');
    }
    
    function setup($ticketId = 0) {
        if ($ticketId > 0) {
            $this->set('ticket_id', $ticketId);
            $this->populate();
        }
    }    
     /**
     * Close a ticket by update the closed field.
     * @param $ticketId
     * @return nothing
     */
    public function close($ticketId) {
        $sql = "UPDATE ticket SET closed = NOW() WHERE ticket_id = $ticketId";
        $this->_execute($sql);
    }
    /**
     * Add a new ticket for a patient.
     * @param $patientId
     * @param $departmentId
     * @return nothing.
     */
    public function addNewTicket($patientId, $departmentId) {
        $insurance = Celini::newORDO('Insurance')->insuranceType($patientId);
        if ($insurance === false) {
            $insurance = 'no';      
        }
        $patientType = Utils::getEnumKey(EnumerationConstant::PATIENT_TYPE, EnumerationConstant::PATIENT_TYPE_INPATIENT);
            
        $data = array (                                         
                'name'          => "$this->_ticket_prefix " . date('Y-m-d'),
                'insurance'     => $insurance,
                'status'        => Ticket::OPEN_STATUS,
                'patient_id'    => $patientId,
                'department_id' => $departmentId,
                'enum_patient_type'=> $patientType 
                       
            );      
        $this->populateArray($data);        
        $this->persist();
        $ticketId = $this->get_id();
        $sql = "UPDATE ticket set created = NOW(), started = NOW() where ticket_id = " . $this->get_id();
        $this->_execute($sql);  
        
        //create default medical record
        $emr = Celini::newORDO('InPatientEmr');
        $data = array('ticket_id'=>$ticketId);
        $emr->recordEmrInfo($data);
        
    }
 /**
     * Get all ticket info.
     * @param $ticketId
     * @return array(in_patient_id,patient_id, department_id, patient_name, ticket_id, patient_status, ticket_status)
     */
    public function getTicketInfo($ticketId) {
        $ds = new Datasource_sql();        
        $sql = array(
            'cols'  => "IP.in_patient_id in_patient_id,T.patient_id  patient_id, T.department_id  department_id, PE.name patient_name, IP.ticket_id ticket_id, IP.enum_status patient_status,               
                    PA.patient_code patient_code, PA.relative_name , T.started check_in_time, T.status ticket_status",
            'from'  => " in_patient IP " .
                    " INNER JOIN ticket T ON IP.ticket_id = T.ticket_id " .
                    " INNER JOIN patient PA ON T.patient_id = PA.person_id " .
                    " INNER JOIN person PE ON T.patient_id = PE.person_id ", 
            'where' => "T.ticket_id = $ticketId "            
            );                  
        
        $ds->setup($this->_db, $sql); 
        $result = $ds->toArray();
        if (empty($result)) return null;
        
        return $result[0];      
                
    }
    
    /**
     * get all the opent ticket of a inpatient [$inPatientId].
     * @param $ticketId
     * @return array(in_patient_id,patient_id, department_id, patient_name, ticket_id, patient_status)
     */
    public function getOpenTicketsInfo($inPatientId) {
        $ds = new Datasource_sql();        
        $sql = array(
            'cols'  => "IP.in_patient_id in_patient_id,T.patient_id  patient_id, T.department_id  department_id, PE.name patient_name, IP.ticket_id ticket_id, IP.enum_status enum_status,               
                    PA.patient_code patient_code, PA.relative_name , T.started check_in_time",
            'from'  => " in_patient IP " .
                    " INNER JOIN ticket T ON IP.ticket_id = T.ticket_id " .
                    " INNER JOIN patient PA ON T.patient_id = PA.person_id " .
                    " INNER JOIN person PE ON T.patient_id = PE.person_id ", 
            'where' => "T.patient_id = $inPatientId AND T.status = '" . Ticket::OPEN_STATUS . "' "            
            );                  
        
        $ds->setup($this->_db, $sql); 
        $result = $ds->toArray();
        if (empty($result)) return null;
        
        return $result;      
                
    }    
}
?>